package com.xinai.dao;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;


import com.xinai.vo.BarCode;
import com.xinai.vo.XnBarCode;

import java.math.BigDecimal;

public class XnBarCodeDao extends BaseDao {
    private static final String TABLE = "XnBarCode";
    public XnBarCodeDao(Context context){
        super(context);
    }
    public XnBarCode find(final String barcode, final String customerId, final String type) {
        return callBack(TYPE_READ, new DaoCallBack<XnBarCode>() {
            @Override
            public XnBarCode invoke(SQLiteDatabase conn) {
                cursor = conn.query(TABLE, null, " BarCode = ? and CustomerId = ? and Type = ?", new String[] {barcode.toLowerCase(), customerId, type}, null, null, null);
                while (cursor.moveToNext()) {
                    XnBarCode barcode = new XnBarCode();
                    barcode.setBarCode(cursor.getString(cursor.getColumnIndex("BarCode")));
                    barcode.setProductID(cursor.getString(cursor.getColumnIndex("productID")));
                    barcode.setProductNo(cursor.getString(cursor.getColumnIndex("productNo")));
                    barcode.setProductName(cursor.getString(cursor.getColumnIndex("productName")));
                    barcode.setColorNo(cursor.getString(cursor.getColumnIndex("colorNo")));
                    barcode.setColorName(cursor.getString(cursor.getColumnIndex("ColorName")));
                    barcode.setColorID(cursor.getString(cursor.getColumnIndex("ColorID")));
                    barcode.setSizesID(cursor.getString(cursor.getColumnIndex("SizesID")));
                    barcode.setSizesName(cursor.getString(cursor.getColumnIndex("SizesName")));
                    barcode.setSizesNo(cursor.getString(cursor.getColumnIndex("SizesNo")));
                  //  barcode.setIndexno(cursor.getInt(cursor.getColumnIndex("IndexNo")));
                    barcode.setType(cursor.getString(cursor.getColumnIndex("Type")));
                    barcode.setCustomerId(cursor.getString(cursor.getColumnIndex("CustomerId")));
                    barcode.setRetailPrice(new BigDecimal(cursor.getDouble(cursor.getColumnIndex("RetailPrice"))));
                    barcode.setUnitPrice(new BigDecimal(cursor.getDouble(cursor.getColumnIndex("UnitPrice"))));
                    barcode.setDiscount(new BigDecimal(cursor.getDouble(cursor.getColumnIndex("Discount"))));
                    barcode.setDiscountPrice(new BigDecimal(cursor.getDouble(cursor.getColumnIndex("DiscountPrice"))));
                  //  barcode.setSizeGroupId(cursor.getString(cursor.getColumnIndex("SizeGroupID")));
                    return barcode;
                }
                return null;
            }
        });
    }

    public String findBarCode(final String barcode, final String customerId, final String type) {
        return callBack(TYPE_READ, new DaoCallBack<String>() {
            @Override
            public String invoke(SQLiteDatabase conn) {
                cursor = conn.query(TABLE, null, " BarCode = ? and CustomerId = ? and Type = ?", new String[] {barcode.toLowerCase(), customerId, type}, null, null, null);
                while (cursor.moveToNext()) {
                    return cursor.getString(cursor.getColumnIndex("BarCode"));
                }
                return null;
            }
        });
    }

    public int insert(final XnBarCode barcode) {
        return callBack(TYPE_WRITE, new DaoCallBack<Integer>() {
            @Override
            public Integer invoke(SQLiteDatabase conn) {
                conn.execSQL("insert into XnBarCode(BarCode,productID,productNo,productName,colorNo,ColorName,ColorID,SizesID,SizesName,SizesNo,RetailPrice,CustomerId,Type,UnitPrice,Discount,DiscountPrice) values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)", new Object[] {
                        barcode.getBarCode().toLowerCase(), barcode.getProductID(), barcode.getProductNo(), barcode.getProductName(), barcode.getColorNo(), barcode.getColorName(), barcode.getColorID(), barcode.getSizesID(), barcode.getSizesName(), barcode.getSizesNo(),
                         barcode.getRetailPrice(), barcode.getCustomerId(), barcode.getType(), barcode.getUnitPrice(), barcode.getDiscount(),barcode.getDiscountPrice()});
                return 1;
            }
        });
    }

    public int update(final BigDecimal discountPrice, final String barcode, final String customerId, final String type) {
        return callBack(TYPE_WRITE, new DaoCallBack<Integer>() {
            @Override
            public Integer invoke(SQLiteDatabase conn) {
                conn.execSQL("update BarCode set DiscountPrice = ? where GoodsID = ? and CustomerId = ? and Type = ?", new Object[] {discountPrice, barcode.toLowerCase(), customerId, type});
                return 1;
            }
        });
    }





}
